package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    @Transactional
    public void saveWithDish(SetmealDto dto) {
        //1. 保存套餐的原有信息
        save(dto);
        //2. 保存套餐和菜品的关联信息
        Long setmealId = dto.getId();// 获取套餐id，并设置到关联集合中
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
        //上传到数据库
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public SetmealDto getByIdWithFlavor(Long id) {
        Setmeal setmeal = getById(id);
        SetmealDto setmealDto = new SetmealDto();
        //将套餐的单表信息添加到Dto
        BeanUtils.copyProperties(setmeal,setmealDto);
        //select categoryName from category where id = categoryId 查套餐的分类
        //select * from setmeal_dish where setmeal_id =? 查套餐关联的菜品
        LambdaQueryWrapper<SetmealDish> dLqw = new LambdaQueryWrapper<>();
        dLqw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(dLqw);
        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(SetmealDto setmealDto) {
        updateById(setmealDto);//先修改setmeal的单表信息
        Long id = setmealDto.getId();
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        //查找SetmealDish的相关信息
        lqw.eq(SetmealDish::getSetmealId,id);
        //先删再加
        setmealDishService.remove(lqw);
        List<SetmealDish> list = setmealDto.getSetmealDishes();
        list.forEach(setmealDish->setmealDish.setSetmealId(id));
        setmealDishService.saveBatch(list);
    }


    /**
     * 修改售卖状态
     * @param status
     * @param ids
     */
    @Override
    public void updateByStatus(Integer status, List<Long> ids) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId,ids);
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        update(setmeal,lqw);
    }

    /**
     * 根据id集合删除
     */
    @Override
    public void deleteByIds(List<Long> ids) {
        //1. 判断这些套餐中是否包含启售的套餐，但凡有一个，不允许删除，抛出业务异常
        //select count(*) from setmeal where id in(?,?) and status=1
        LambdaQueryWrapper<Setmeal> setmealLqw = new LambdaQueryWrapper<>();
        setmealLqw.in(Setmeal::getId, ids);
        setmealLqw.eq(Setmeal::getStatus, 1);
        int count = count(setmealLqw);
        if(count > 0) {
            //当前套餐中存在启售套餐，不允许删除
            throw new CustomException("存在启售套餐，不允许删除");
        }
        //2. 删除套餐基本信息 delete from setmeal where id in (?,?)
        removeByIds(ids);
        //3. 删除套餐和菜品的关联表
        //delete from setmeal_dish where setmeal_id in (?,?)
        LambdaQueryWrapper<SetmealDish> sdLqw = new LambdaQueryWrapper<>();
        sdLqw.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(sdLqw);
    }


}
